#include "EncryptChar.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"

//double random(double start, double end)
//{
//    return start+(end-start)*rand()/(RAND_MAX + 1.0);
//}

EncryptChar::EncryptChar(const char* src)
:_srcChar(NULL)
,_retChar(NULL)
{
    size_t len = strlen(src);
    _srcChar = new char[len+1];
    strcpy(_srcChar, src);
//    for(int i=0; i<len; i++)
//        *(_srcChar+i) = _decrypt(*(src+i), _keys[i%5]);
    printf("encrypt src = %s\n", src);
    printf("encrypt srcChar = %s\n", _srcChar);
}

EncryptChar::~EncryptChar()
{
    delete [] _srcChar;
    if(_retChar){
        delete [] _retChar;
    }
}

const char* EncryptChar::decrypt()
{
    if(_retChar == nullptr){
        size_t len = strlen(_srcChar);
        _retChar = new char[len+1];
		int _keys[5] = { 1, 3, 5, 7, 9 };
        for(int i=0; i<len; i++)
            *(_retChar+i) = _decrypt(*(_srcChar+i), _keys[i%5]);
    }
    printf("decrypt retChar = %s\n", _retChar);
    return _retChar;
}

int EncryptChar::getLen()
{
    return int(strlen(_srcChar));
}

char EncryptChar::_encrypt(char c, int key)
{
    char ret = c^key;
	return ret;
}

char EncryptChar::_decrypt(char c, int key)
{
    char ret = c^key;
    return ret;
}

